public class p1016_563ms_3rd {

	static//
	// -----
	// -----
	public class Main {
		public static void main(String[] args) throws Exception {
			java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			String num;
			while (!(num = reader.readLine()).startsWith("-")) {
				int ret = process(num);
				if (ret == 0) {
					System.out.println(num + " is self-inventorying");
				} else if (ret > 0) {
					System.out.println(num + " is self-inventorying after " + ret + " steps");
				} else if (ret > -100) {
					System.out.println(num + " enters an inventory loop of length " + -ret);
				} else {
					System.out.println(num + " can not be classified after " + maxIteration + " iterations");
				}
			}
		}

		static final int maxIteration = 15;

		static int process(String num) {
			java.util.Map<String, Integer> total = new java.util.HashMap<String, Integer>();
			total.put(num, 0);
			for (int i = 1; i <= maxIteration; i++) {
				num = iterate(num);
				if (total.containsKey(num)) {
					int met = total.get(num);
					int loop = i - met;
					return loop == 1 ? met : -loop;
				}
				total.put(num, i);
			}
			return -100;
		}

		static String iterate(String num) {
			int[] count = new int[10];
			for (char c : num.toCharArray())
				count[c - '0']++;
			StringBuilder out = new StringBuilder();
			for (int i = 0; i <= 9; i++) {
				int x = count[i];
				if (x > 0)
					out.append(x).append(i);
			}
			return out.toString();
		}
	}

	// -----
	// -----

	public static void main(String[] args) throws Exception {
		Judge.run();
	}

}
